<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料名称" prop="mtrName">
        <el-input v-model="queryParams.mtrName" placeholder="请输入物料名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="需求数量" prop="needQuantity">
        <el-input v-model="queryParams.needQuantity" placeholder="请输入需求数量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['mes:need:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['mes:need:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mes:need:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['mes:need:export']">导出</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="needList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="" align="center" prop="needId" /> -->
      <el-table-column label="物料计划编码" align="center" prop="buyNeed.mtrPlanCode" />
      <el-table-column label="采购需求编码" align="center" prop="buyNeed.needCode" />
      <el-table-column label="原料名称" align="center" prop="mtrs" />
      <el-table-column label="总价" align="center" prop="buyNeed.status">
        <template slot-scope="scope">
{{ (scope.row.totalCost).toFixed(2)+"元" }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="原料编码" align="center" prop="mtrInfos.proCode" /> -->
      <el-table-column label="状态" align="center" prop="buyNeed.status">
        <template slot-scope="scope">
          <el-tag :type="tagType(scope.row.buyNeed.status)" effect="dark">
            <dict-tag :options="dict.type.mes_buyneed_status" :value="scope.row.buyNeed.status" />
          </el-tag>

        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="buyNeed.remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.buyNeed.status == 0" size="mini" type="text" icon="el-icon-edit"
            @click="shengchengdindan(scope.row)" v-hasPermi="['mes:need:edit']">生成采购订单</el-button>

          <el-button size="mini" type="text" icon="el-icon-edit" @click="getDetais(scope.row)"
            v-hasPermi="['mes:need:edit']">详情</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click=""
            v-hasPermi="['mes:need:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['mes:need:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog title="生成采购订单" v-if="createorderopen" :visible.sync="createorderopen" width="500px" append-to-body>
      <el-form ref="form" :rules="rules" label-width="80px">
        <el-form-item label="供应商" prop="mtrName">
          <el-input v-model="supplier" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="需求总价" prop="mtrName">
          {{ needDetaisData.totalCost+'元' }}
        </el-form-item>
        <el-card id="mtrcard" shadow="never" v-for="item in needDetaisData.mtrs" :key="item.mtrCode">

          <div style="display: inline-block;">
            <h4 style="color:#565555;">原料名称: <h2 style="color:black;font-weight: 700;">{{ item.mtrName }}</h2>
            </h4>
            <h4 style="color:#565555;">原料编码: <h3 style="color:black;font-weight: 700;">{{ item.mtrCode }}</h3>
            </h4>
          </div>
          <div style="display: inline-block;position: relative;left: 100px;">
            <!-- <h4 style="color:#565555">采购数量</h4>
            <h1 style="text-align: center;color:black;font-weight: 700;">{{ item.mtrQuantity }}</h1> -->
            <h4 style="color:#565555;">采购数量: <h2 style="color:black;font-weight: 700;">{{ item.mtrQuantity }}</h2>
            </h4>
            <h4 style="color:#565555;">总价: <h3 style="color:black;font-weight: 700;">{{ (item.budgetedCost).toFixed(2) + '元'
            }}</h3>
            </h4>
          </div>
          <div style="display: inline-block;float: right;position: relative;top: 15px;">
            <img style="width: 40%;height: 40%;float: right;" src="@/assets/images/buy.png">
          </div>

        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="createorder">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改采购需求对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料名称" prop="mtrName">
          <el-input v-model="form.mtrName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="需求数量" prop="needQuantity">
          <el-input v-model="form.needQuantity" placeholder="请输入需求数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 采购需求详情对话框 -->
    <el-dialog title="采购需求详情"  :visible.sync="detailOpen" width="500px" append-to-body v-if="detailOpen">
      
      <label style="margin-bottom: 5px;">
        采购需求编码：{{ BuyNeedVo.needCode }}
      </label>
      <label style="margin-bottom: 5px;margin-left: 20px;">
        {{ BuyNeedVo.totalCost+'元' }}
      </label>
        
      <el-divider></el-divider>
      <el-table :data="BuyNeedVo.mtrs" stripe style="width: 100%">
        <el-table-column prop="mtrName" label="原料名称" align="center">
        </el-table-column>
        <el-table-column prop="mtrCode" label="原料编码" align="center">
        </el-table-column>
        <el-table-column prop="mtrQuantity" label="数量" align="center">
        </el-table-column>
        <el-table-column prop="budgetedCost" label="总价" align="center">
          <template slot-scope="scope">
            {{ (scope.row.budgetedCost).toFixed(2) + '元' }}
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="detailOpen = false">确 定</el-button>
        <el-button @click="detailOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNeed, getNeed, delNeed, addNeed, updateNeed, createBuyorder, getDetais } from "@/api/mes/need";

export default {
  name: "Need",
  dicts: ['mes_buyneed_status'],
  data() {
    return {
      //采购需求详情
      BuyNeedVo: [],
      //采购需求详情对话框
      detailOpen: false,
      //供应商
      supplier: "",
      //采购需求详情
      needDetaisData: [],
      //当前点击记录id
      nowid: "",
      //生成采购订单对话框
      createorderopen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购需求表格数据
      needList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mtrName: null,
        needQuantity: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
  
    //查询采购需求详情
    getDetais(row) {
      // console.log(row)
      getDetais({ id: row.buyNeed.needId }).then(res => {
        this.BuyNeedVo = res.data
        this.detailOpen = true
      })
    },
    //标签类型判断
    tagType(status) {
      switch (status) {
        case 0:
          return ""
        case 1:
          return "success"
      }
    },
    //生成采购订单
    createorder() {
      createBuyorder({ needId: this.nowid, supplier: this.supplier }).then(res => {
        this.createorderopen = false
        this.getList()
        this.$modal.msgSuccess("采购订单生成成功");
      })
    },
    //生成采购订单按钮
    shengchengdindan(row) {
      this.nowid = row.buyNeed.needId
      getDetais({ id: row.buyNeed.needId }).then(res => {
        this.needDetaisData = res.data
      })
      this.createorderopen = true
    },
    /** 查询采购需求列表 */
    getList() {
      this.loading = true;
      listNeed(this.queryParams).then(response => {
        this.needList = response.rows;
        this.needList.forEach((item, index) => {
          let mtrs = [];
          item.mtrInfos.forEach(e => {
            mtrs.push(e.mtrName)
          })
          this.needList[index].mtrs = mtrs.join();

        })
        this.total = response.total;
        this.loading = false;

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.createorderopen = false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        needId: null,
        needCode: null,
        progId: null,
        mtrPlanId: null,
        mtrName: null,
        needQuantity: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.needId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购需求";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const needId = row.needId || this.ids
      getNeed(needId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购需求";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.needId != null) {
            updateNeed(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNeed(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const needIds = row.needId || this.ids;
      this.$modal.confirm('是否确认删除采购需求编号为"' + needIds + '"的数据项？').then(function () {
        return delNeed(needIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/need/export', {
        ...this.queryParams
      }, `need_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
#mtrcard>>>.el-card__body {
  padding-bottom: unset;

}

#mtrcard {
  margin-bottom: 15px;
  border-radius: 10px;
  box-shadow: 3px 3px 5px rgba(128, 128, 128, 0.699);
  background-image: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}
</style>